<template>
  <div class="relative w-full h-full">
    <PageHeader :title="$t('discountActivity.折扣活动管理')" />
    <div class="flex_aic_jcsb">
      <div class="flex_aic gap-4">
        <el-input
          v-model="searchForm.name"
          :placeholder="$t('discountActivity.请输入活动名称')"
          style="width: 200px"
        >
          <template #prefix>
            <span class="color-555555">{{ $t('discountActivity.活动名称') }}</span>
          </template>
        </el-input>
        <el-input
            v-model="searchForm.tenantName"
            :placeholder="$t('discountActivity.店铺名称')"
            style="width: 200px"
        >
          <template #prefix>
            <span class="color-555555">{{ $t('discountActivity.店铺名称') }}</span>
          </template>
        </el-input>

        <el-date-picker
          v-model="searchForm.timeRange"
          type="daterange"
          :range-separator="$t('datePick.至')"
          :start-placeholder="$t('discountActivity.开始日期')"
          :end-placeholder="$t('discountActivity.结束日期')"
          style="width: 350px"
        />
      </div>
      <div class="flex_aic">
        <el-button @click="resetSearch">{{ $t('discountActivity.重置') }}</el-button>
        <el-button type="primary" @click="getTableData">{{ $t('discountActivity.查询') }}</el-button>
      </div>
    </div>

    <div class="mt12 toolbar flex_aic_jcsb" v-my-point="'skuActivity:add'">
      <div class="addBtn flex_aic_jcc" @click="handleAdd">
        <SvgIcon name="iconxinzeng" class="mr8" />
        <span>{{ $t('discountActivity.新增活动') }}</span>
      </div>
    </div>

    <el-table
      :data="tableData"
      style="width: 100%"
      class="cover-table mt12"
      height="calc(100% - 200px)"
      border
      v-loading="loading"
    >
      <el-table-column :prop="'name'" :label="$t('discountActivity.活动名称')" />
      <el-table-column :label="$t('discountActivity.店铺名称')">
        <template #default="{ row }">
          {{ row["tenantName" + locale] }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('discountActivity.活动时间范围')">
        <template #default="{ row }">
          {{ moment(row.startTime).format("YYYY-MM-DD HH:mm:ss") }} {{ $t('datePick.至') }}
          {{ moment(row.endTime).format("YYYY-MM-DD HH:mm:ss") }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('discountActivity.折扣比例')">
        <template #default="scope"> {{ scope.row.discountRate }}% </template>
      </el-table-column>
      <el-table-column :label="$t('discountActivity.活动状态')">
        <template #default="scope">
          <el-tag :type="statusMap?.[scope.row.status]?.type">
            {{ $t(`discountActivity.${statusMap?.[scope.row.status]?.label}`) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column :label="$t('discountActivity.操作')" width="250">
        <template #default="scope">
          <div class="operationBtnRow">
            <!-- <el-button type="text" size="small" @click="handleEdit(scope.row)">
              编辑
            </el-button> -->
            <el-button
              type="text"
              size="small"
              @click="handleDetail(scope.row)"
            >
              {{ $t('discountActivity.详情') }}
            </el-button>
            <el-popconfirm
              :title="$t('discountActivity.确定删除该活动吗')"
              @confirm="handleRemove(scope.row)"
              v-if="scope.row.status != 1"
              width="300px"
            >
              <template #reference>
                <el-button
                  type="text"
                  size="small"
                  v-my-point="'skuActivity:del'"
                  >{{ $t('discountActivity.删除') }}</el-button
                >
              </template>
            </el-popconfirm>
            <el-button
              type="text"
              size="small"
              @click="handleStop(scope.row)"
              v-if="scope.row.status != -1 && scope.row.status != 2"
              v-my-point="'skuActivity:stop'"
            >
              {{ $t('discountActivity.终止') }}
            </el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <div class="flex justify-end mt24 absolute bottom-0 right-0">
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[10, 20, 30, 40]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>

    <editDia
      ref="editDiaRef"
      v-model="showEditDia"
      @success="handleRefresh"
      :data="editData"
    />
    <addDia
      ref="addDiaRef"
      v-model="showAddDia"
      @success="handleRefresh"
      :data="editData"
    />
  </div>
</template>

<script setup>
import { ref, onMounted } from "vue";
import editDia from "./editDia.vue"; //详情弹窗
import addDia from "./addDia.vue"; //新增弹窗/编辑弹窗
import Api from "@/config/api/api";
import moment from "moment";
import { ElMessage } from "element-plus";
import { useI18n } from "vue-i18n";

const { locale, t } = useI18n();
const searchForm = ref({
  name: "",
  tenantName: "",
  timeRange: [],
});

const addDiaRef = ref(null);
const showAddDia = ref(false);
const editDiaRef = ref(null);
const showEditDia = ref(false);
const editData = ref({});

const currentPage = ref(1);
const pageSize = ref(10);
const total = ref(0);
const loading = ref(false);

const tableData = ref([]);
// -1终止 0未开始 1进行中 2已结束
const statusMap = {
  "-1": {
    label: "终止",
    type: "danger",
  },
  0: {
    label: "未开始",
    type: "warning",
  },
  1: {
    label: "进行中",
    type: "success",
  },
  2: {
    label: "已结束",
    type: "info",
  },
};

const resetSearch = () => {
  currentPage.value = 1;
  pageSize.value = 10;
  searchForm.value = {
    name: "",
    tenantName: "",
    timeRange: [],
  };
  getTableData();
};

const handleSearch = () => {
  // 实现查询逻辑
};

const handleAdd = () => {
  editData.value = {};
  showAddDia.value = true;
};

const handleEdit = (row) => {
  editData.value = { ...row };
  showAddDia.value = true;
};

const handleDetail = (row) => {
  editData.value = { ...row, isView: true };
  showEditDia.value = true;
};

const handleRemove = (row) => {
  Api.skuActivity_delete([row.id]).then((res) => {
    if (res.status === 200) {
      ElMessage.success(t('discountActivity.删除成功'));
      getTableData();
    }
  });
};

const handleStop = (row) => {
  Api.skuActivity_stop(row.id).then((res) => {
    if (res.status === 200) {
      ElMessage.success(t('discountActivity.终止成功'));
      getTableData();
    }
  });
};

const handleSizeChange = (size) => {
  pageSize.value = size;
  getTableData();
};

const handleCurrentChange = (page) => {
  currentPage.value = page;
  getTableData();
};

const handleRefresh = () => {
  currentPage.value = 1;
  getTableData();
};

const getTableData = () => {
  loading.value = true;
  Api.skuActivity_list({
    page: currentPage.value - 1,
    size: pageSize.value,
    startTime: searchForm.value.timeRange?.[0] ? moment(searchForm.value.timeRange[0]).format("YYYY-MM-DD 00:00:00") : "",
    endTime: searchForm.value.timeRange?.[1] ? moment(searchForm.value.timeRange[1]).format("YYYY-MM-DD 23:59:59") : "",
    name: searchForm.value.name,
    ['tenantName' + locale.value]: searchForm.value.tenantName,
  })
    .then((res) => {
      tableData.value = res.data.content || [];
      total.value = +res.data.totalElements ?? 0;
    })
    .finally(() => {
      loading.value = false;
    });
};

onMounted(() => {
  getTableData();
});
</script>

<style lang="scss" scoped>
.toolbar {
  width: 100%;
  height: 42px;
  margin-bottom: 5px;
  background: rgba(24, 144 ,255, 0.1);
}
.addBtn {
  height: 28px;
  margin-left: 10px;
  padding: 0 7px;
  border-radius: 3px;
  background: #fff;
  cursor: pointer;
}
.cover-table {
  margin-top: 20px;
}
</style>
